// source: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

function initHomeForm() {
	$('.error').hide();
	$('input.text-input').css({backgroundColor:"#FFFFFF"});
	$('input.text-input').focus(function(){
		$(this).css({backgroundColor:"#FFDDAA"});
	});
	$('input.text-input').blur(function(){
		$(this).css({backgroundColor:"#FFFFFF"});
	});

	$("#btn_submit").click(function() {
		// validate and process form
		// first hide any error messages
		$('.error').hide();

		var postBody = $("textarea#postBody").val();
		if (postBody == "") {
			$("label#postBody_error").show();
			$("textarea#postBody").focus();
			return false;
		}

		var d = new Date();
		var testData = "test from json"+d.getTime();
		$.ajaxSetup({async:false});
		$.post(
				//"/~ysahn/seednia-uiphp/index.php/Post/add",
				//"/seednia-uiphp/index.php/Post/json_add",
				//{ creatorSid: 1, postBody: testData },
				"/seednia-webapp/ws/PostServiceRest/insert?accessToken=123",
				{ creatorSid: 1, body: testData },
				function(data) {
					console.log(data);
					//alert("Success:");
					//$('#post_stream').html("<div id='post_"+data.sid+"'>"+data.body+"</div>");
				}, "json"
				)
					// Why is this not working???
				//.success(function() { alert("second success"); })
			    //.error(function(message) { alert("error"+message); console.log(message);})
			    //.complete(function() { alert("complete"); });
		/*
		$.ajax({
			type: "POST",
			url: "/seednia-uiphp/index.php/Post/json_add",
			data: $("#FormPost").serialize(),
			success: function() {
				alert("Success:"+data);
			}
		});
		*/
	});
	return false;
};
